package com.keba.lamp.complaint.service.impl;

import com.keba.basic.base.service.impl.SuperServiceImpl;
import com.keba.basic.database.mybatis.conditions.Wraps;
import com.keba.basic.database.mybatis.conditions.query.LbQueryWrap;
import com.keba.basic.utils.ArgumentAssert;
import com.keba.basic.utils.BeanPlusUtil;
import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.complaint.entity.EvidenceComponent;
import com.keba.lamp.complaint.entity.EvidenceComponentCaseTypeRel;
import com.keba.lamp.complaint.manager.EvidenceComponentCaseTypeRelManager;
import com.keba.lamp.complaint.manager.EvidenceComponentManager;
import com.keba.lamp.complaint.service.EvidenceComponentService;
import com.keba.lamp.complaint.vo.result.EvidenceComponentResultVO;
import com.keba.lamp.complaint.vo.save.EvidenceComponentSaveVO;
import com.keba.lamp.complaint.vo.update.EvidenceComponentUpdateVO;
import com.keba.lamp.file.service.AppendixService;
import com.keba.lamp.file.service.FileService;
import com.keba.lamp.model.vo.result.AppendixResultVO;
import com.keba.lamp.model.vo.save.AppendixSaveVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 证据组件库
 * </p>
 *
 * @author chaowu.wang
 * @date 2025-03-27 20:34:20
 * @create [2025-03-27 20:34:20] [chaowu.wang] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EvidenceComponentServiceImpl extends SuperServiceImpl<EvidenceComponentManager, Long, EvidenceComponent> implements EvidenceComponentService {

    private final AppendixService appendixService;
    private final FileService fileService;
    private final EvidenceComponentCaseTypeRelManager evidenceComponentCaseTypeRelManager;

    @Override
    public EvidenceComponentResultVO getComponentById(Long id) {
        EvidenceComponent entity = this.getSuperManager().getById(id);
        EvidenceComponentResultVO resultVO = BeanPlusUtil.toBean(entity, EvidenceComponentResultVO.class);
        AppendixResultVO appendixResultVO = appendixService.getByBiz(entity.getId(), AppendixType.Complaint.COMPLAINT__EVIDENCE__SAMPLE__FILE);
        if (Objects.nonNull(appendixResultVO)) {
            String url = fileService.findUrlById(appendixResultVO.getId());
            resultVO.setSamplePicUrl(url);
        }
        return resultVO;
    }

    @Override
    public List<EvidenceComponentResultVO> getComponentByIds(List<Long> ids) {
        List<EvidenceComponent> entityList = this.getSuperManager().listByIds(ids);
        List<EvidenceComponentResultVO> resultVOList = BeanPlusUtil.toBeanList(entityList, EvidenceComponentResultVO.class);
        resultVOList.stream().forEach(resultVO -> {
            AppendixResultVO fileVO = appendixService.getByBiz(resultVO.getId(), AppendixType.Complaint.COMPLAINT__EVIDENCE__SAMPLE__FILE);
            if (Objects.nonNull(fileVO)) {
                String url = fileService.findUrlById(fileVO.getId());
                resultVO.setSamplePicUrl(url);
            }
        });
        return resultVOList;
    }

    @Override
    protected <SaveVO> EvidenceComponent saveBefore(SaveVO saveVO) {
        EvidenceComponentSaveVO evidenceComponentSaveVO = (EvidenceComponentSaveVO) saveVO;
        String maxCode = this.superManager.selectMaxCode();
        if (StringUtils.isNotBlank(maxCode)) {
            evidenceComponentSaveVO.setCode(String.valueOf(Integer.parseInt(maxCode) + 1));
        } else {
            evidenceComponentSaveVO.setCode("1000");
        }
        return super.saveBefore(saveVO);
    }

    @Override
    protected <SaveVO> void saveAfter(SaveVO saveVO, EvidenceComponent evidenceComponentEntity) {
        EvidenceComponentSaveVO evidenceComponentSaveVO = (EvidenceComponentSaveVO) saveVO;
        Long fileId = evidenceComponentSaveVO.getFileId();
        if (Objects.nonNull(fileId)) {
            AppendixSaveVO appendixSaveVO = AppendixSaveVO.build(evidenceComponentEntity.getId(), AppendixType.Complaint.COMPLAINT__EVIDENCE__SAMPLE__FILE,
                    fileId);
            appendixService.save(appendixSaveVO);
        }
    }

    @Override
    protected <UpdateVO> void updateAfter(UpdateVO updateVO, EvidenceComponent evidenceComponentEntity) {
        EvidenceComponentUpdateVO evidenceComponentUpdateVO = (EvidenceComponentUpdateVO) updateVO;
        Long fileId = evidenceComponentUpdateVO.getFileId();
        if (Objects.isNull(fileId)) {
            appendixService.listByBizIdAndBizType(evidenceComponentEntity.getId(), AppendixType.Complaint.COMPLAINT__EVIDENCE__SAMPLE__FILE);
        } else {
            AppendixSaveVO appendixSaveVO = AppendixSaveVO.build(evidenceComponentEntity.getId(), AppendixType.Complaint.COMPLAINT__EVIDENCE__SAMPLE__FILE,
                    fileId);
            appendixService.save(appendixSaveVO);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        ids.forEach(id -> {
            //EvidenceComponent evidenceComponent = this.getById(id);
            //ArgumentAssert.notNull(evidenceComponent, "未找到对应的证据组件");
            LbQueryWrap<EvidenceComponentCaseTypeRel> queryWrap = Wraps.<EvidenceComponentCaseTypeRel>lbQ()
                    .like(EvidenceComponentCaseTypeRel::getComponentId, id);
            long count = evidenceComponentCaseTypeRelManager.count(queryWrap);
            ArgumentAssert.isTrue(count < 1, "组件已被证据目录使用无法删除");
        });
        appendixService.removeByBizId(ids, AppendixType.Complaint.COMPLAINT__EVIDENCE__SAMPLE__FILE);
        return removeByIds(ids);
    }
}


